<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

/**
 * Class CostAllocation
 * Desc  城市费用配置控制器
 * Create on 2024/10/30 9:03
 * Create by wangyafang
 */
namespace app\api\controller\wddj\user\operation;

use app\api\controller\wddj\BaseController;
use app\api\controller\wddj\user\Map;
use app\api\model\wddj\user\operation\StartingPrice;

class CostAllocation extends BaseController
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $model = null;
    protected $city_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\user\operation\CostAllocation();
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $city = $this->request->get('city');
        $cityObj = $this->validateCity($city);
        $this->city_id = $cityObj['id'];
    }

    /**
     * Desc 计价规则
     * Create on 2024/10/30 9:44
     * Create by wangyafang
     */
    public function pricing_rules()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $where['city_id'] = array('eq',$this->city_id);
        $startPriceData = (new StartingPrice())
            ->where($where)
            ->order('weigh asc,id asc')
            ->field('start_time,end_time,price,kilometer')
            ->select();
        $costAllocationObj = $this->model->where($where)->find();
        $costAllocationObj->hidden(['id','city_id']);
        $cityObj = (new \app\api\model\wddj\user\operation\City())->get($this->city_id);

        $data = array(
            'city'=>$cityObj->city,
            'starting_price_list'=>$startPriceData,
            'mileage_fee'=>array(
                'mileage_fee_start_time'=>$costAllocationObj['mileage_fee_start_time'],
                'mileage_fee_end_time'=>$costAllocationObj['mileage_fee_end_time'],
                'mileage_price'=>$costAllocationObj['mileage_price'],
            ),
            'long_distance'=>array(
                'long_distance_fee_start_time'=>$costAllocationObj['long_distance_fee_start_time'],
                'long_distance_fee_end_time'=>$costAllocationObj['long_distance_fee_end_time'],
                'capped_price'=>$costAllocationObj['capped_price'],
                'long_distance_price'=>$costAllocationObj['long_distance_price'],
                'exceeding_kilometers'=>$costAllocationObj['exceeding_kilometers'],
            ),
            'duration'=>array(
                'free_waiting_time'=>$costAllocationObj['free_waiting_time'],
                'maximum_charging_duration'=>$costAllocationObj['maximum_charging_duration'],
                'duration_price'=>$costAllocationObj['duration_price'],
                'duration_fee_start_time'=>$costAllocationObj['duration_fee_start_time'],
                'duration_fee_end_time'=>$costAllocationObj['duration_fee_end_time'],
            ),
        );
        $this->success('请求成功',$data);
    }

    /**
     * Desc 获取预估价格
     * Create on 2024/10/25 14:21
     * Create by wangyafang
     */
    public function get_estimated_amount()
    {

        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $param = $this->request->get();
        $from_lat = $param['from_lat'];//出发地纬度
        $from_lng = $param['from_lng'];
        $to_lat = $param['to_lat'];
        $to_lng = $param['to_lng'];

        $mapResult = (new Map())->get_driver_routes($param);

        $city_id = $this->city_id;


        $routes = $mapResult['result']['routes'];

        $estimated_amount_result = \app\api\model\wddj\user\operation\CostAllocation::estimated_amount($city_id,$mapResult['result']['routes'][0]['distance']);

        $data = array(
            'estimated_amount'=>$estimated_amount_result['estimated_amount'],
            'estimated_amount_details'=>$estimated_amount_result['estimated_amount_details'],
            'routes'=>$routes,
        );

        cache('city_id'.$city_id.'from_lat'.$from_lat.'from_lng'.$from_lng.'to_lat'.$to_lat.'to_lng'.$to_lng,$estimated_amount_result['estimated_amount'],7200);

        $this->success('请求成功',$data);
    }
}



 